package com.zsrd.self.handle;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zsrd.self.entity.SelfWxConfig;
import com.zsrd.self.service.SelfWxConfigService;
import com.zsrd.self.util.MySM2Util;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * <pre>
 * 小程序交互操作
 * </pre>
 *
 * @author sunjiyong
 * @since 2024/3/25 14:02
 */
@Component
public class AppletHandle {

    @Autowired
    private SelfWxConfigService selfWxConfigService;


    //获取token
    public String getAccessToken(){
        QueryWrapper qw=new QueryWrapper();
        qw.eq("tag_type",1);
        qw.eq("oper_flag","getAccessToken");
        SelfWxConfig wxConfig = selfWxConfigService.getOne(qw);
        String url = String.format(wxConfig.getPostUrl(),wxConfig.getAppId(), MySM2Util.decrypt(wxConfig.getAppSecret()));
        try {
            HttpClient client = HttpClients.createDefault(); // 构建一个Client
            HttpGet get = new HttpGet(url); // 构建一个Post请求
            HttpResponse response = client.execute(get); // 提交get请求
            HttpEntity result = response.getEntity(); // 拿到返回的HttpResponse的"实体"
            String content = EntityUtils.toString(result);
            // 返回JSON格式的响应字符串
            return content;
        } catch (Exception e) {
            e.printStackTrace();
            // 返回错误信息
            return "{\"error\": \"" + e.getMessage() + "\"}";
        }
    }


    //获取小程序跳转链接
    public String getGenerateScheme(String jumpWx){
        QueryWrapper qw=new QueryWrapper();
        qw.eq("tag_type",1);
        qw.eq("oper_flag","generateScheme");
        SelfWxConfig wxConfig = selfWxConfigService.getOne(qw);
        String accessTokenStr=getAccessToken();
        JSONObject  accessTokenJson=JSON.parseObject(accessTokenStr);
        String accessToken=accessTokenJson.getString("access_token");
        String url = String.format(wxConfig.getPostUrl(),accessToken);
        try {
            HttpClient client = HttpClients.createDefault(); // 构建一个Client
            HttpPost post = new HttpPost(url); // 构建一个Post请求
            post.setHeader("Content-Type", "application/json"); // 设置请求头为JSON

            // 创建一个 JSON 格式的请求主体
            JSONObject jsonRequest = new JSONObject();
            jsonRequest.put("jump_wxa", JSON.parseObject(jumpWx));
            jsonRequest.put("is_expire", true);
            jsonRequest.put("expire_type", 1);
            jsonRequest.put("expire_interval", 1);
            StringEntity input = new StringEntity(jsonRequest.toString(), "UTF-8");
            post.setEntity(input);

            HttpResponse response = client.execute(post); // 提交POST请求
            HttpEntity result = response.getEntity(); // 拿到返回的HttpResponse的"实体"
            String content = EntityUtils.toString(result);
            // 返回JSON格式的响应字符串
            return content;
        } catch (Exception e) {
            e.printStackTrace();
            // 返回错误信息
            return "{\"error\": \"" + e.getMessage() + "\"}";
        }
    }


    //获取code2Session
    public  String getCode2Session(String jsCode) {
        QueryWrapper qw=new QueryWrapper();
        qw.eq("tag_type",1);
        qw.eq("oper_flag","code2Session");
        SelfWxConfig wxConfig = selfWxConfigService.getOne(qw);
        String url = String.format(wxConfig.getPostUrl(),wxConfig.getAppId(),MySM2Util.decrypt(wxConfig.getAppSecret()),jsCode);
        try {
            HttpClient client = HttpClients.createDefault(); // 构建一个Client
            HttpGet get = new HttpGet(url); // 构建一个Post请求
            HttpResponse response = client.execute(get); // 提交get请求
            HttpEntity result = response.getEntity(); // 拿到返回的HttpResponse的"实体"
            String content = EntityUtils.toString(result);
            // 返回JSON格式的响应字符串
            return content;
        } catch (Exception e) {
            e.printStackTrace();
            // 返回错误信息
            return "{\"error\": \"" + e.getMessage() + "\"}";
        }
    }


}
